package com.fit.pages.company;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.company.CompanyDAO;
import com.fit.entities.Company;

@AdminAccess
public class EditCompany {

	@Property
	@Persist
	private Company company;
	
	@Inject
	private CompanyDAO companyDAO;
	
	@InjectPage
	private ManageCompanies manage;


	Object onActivate(Long id) {
		manage.setError("");
		manage.setConfirmation("");
		company = companyDAO.findById(id);
		if (company == null) {
			manage.setError("Error.");
			return manage;
		}
		return null;
	}

	Object onSuccess() {
		manage.setError("");
		manage.setConfirmation("");
		try {
			companyDAO.update(company);
			manage.setConfirmation("Successfully edited.");
			return manage;
		} catch (Exception e) {
			manage.setError("Error.");
			return manage;
		}

	}

}
